import commonList from '@/components/common/tList'
import tree from '@/components/tki-tree/companyTree'

export default {

	data() {
		return {
			Inv: 0,
			index: -1,
			// 被检查单位
			multiple: false,
			value: 0,
			selectParent: true,
			flod: true,
			companyTree: [],
			treeName: null,
			bhjNameList: [],
			placeholderText: '请先选择标段',
			pickerIndex: -1,
			disabled: true,
			nData: null,
			selectList: [],
			// 新增
			num: 0,
			dataList: {
				endDates: "",
				fCloseoperation: "",
				fCompanyid: "",
				keyWord: "",
				machineId: "",
				page: 1,
				pageSize: 50,
				startDates: "",
				stationId: "",
				strengthgrade: "",
				warningLevel: "",
			},
			// 未提交数组
			tableData: [],
			userInfo: {},
			AduitUser: null, //审核流程
			companyId: null,
			table_config: [{
					label: "闭合状态",
					prop: "F_CloseStatus",
					isLine: false,
				},
				{
					label: "生产任务单号",
					prop: "F_CloseOperation",
					isLine: false,
				},
				{
					prop: "level",
					label: "预警级别",
					isLine: true,
				},
				{
					label: "生产任务单号",
					prop: "F_ProduceTaskNumber",
					isLine: true,
				},
				{
					label: "工程部位",
					prop: "F_EngineeringPartName",
					isLine: true,
				},
				{
					label: "上传时间",
					prop: "F_DischargeDates",
					isLine: true,
				},
				{
					label: '出料时间',
					prop: 'dischargeDates',
					isLine: true,
				}
			],
		}
	},
	components: {
		commonList,
		tree
	},
	onShow() {
		var that = this
		if (this.Inv == 0) {
			setTimeout(() => {
				that.unsubmit()
			}, 300)
		}
		if (this.Inv == 1) {
			setTimeout(() => {
				that.submit()
			}, 300)
		}
	},
	onLoad(option) {
		var that = this
		if (option.Inv) {
			this.Inv = option.Inv
		}
		this.getLocalData()
	},
	// 新增
	onNavigationBarButtonTap(e) {
		if (e.index === 0) {
			var url = "/pages/apply/station/yjTensionExceedData/inter/index"
			uni.navigateTo({
				url: url
			});
		}
	},
	// 返回
	onBackPress(event) {
		uni.switchTab({
			url: '/pages/home/index'
		});
		return true;
	},

	methods: {
		treeConfirm(e, num) {

			var id;
			var name;
			if (num != 0) {
				id = e[0].fCompanyid
				name = e[0].fShortname
				this.dataList.fCompanyid = e[0].fCompanyid
			} else {
				id = e.fCompanyid
				name = this.treeData[0].fShortname
				this.dataList.fCompanyid = e.fCompanyid
			}

			this.yj_searchForm(this.dataList)
			this.pickerIndex = -1
			this.nData = null
			this.placeholderText = '请选择'
			this.dataList.machineId = ''
			if (id != "") {
				//e为点击机构树后所获得的信息
				this.treeName = name
				this.dataList.fCompanyid = id;
				this.$http.getStationmachineList({
						companyId: id,
						modelType: "0",
						stationType: "1"
					})
					.then((res) => {
						if (res.errcode == 0) {
							this.bhjNameList = ["根据拌合机查询"];
							this.bhjList = res.data.stationmachineList;

							if (this.bhjList.length > 0) {
								this.disabled = false
							} else {
								this.disabled = true
							}
							let bhjName = "";
							this.bhjList.forEach((item) => {
								bhjName = item.fMachinename + "/" + item.fMachineid;
								this.bhjNameList.push(bhjName);
							});
						} else {
							uni.showToast({
								title: "获取拌合机失败",
								icon: 'none',
								duration: 2000
							});
							return false;
						}
					})

			}

		},
		treeCancel() {},

		Picker(e, data) {

			if (data[e.target.value] == "根据张拉机查询") {
				this.dataList.machineId = "";
				this.dataList.stationId = "";
			} else {
				this.nData = data[e.target.value]
				this.pickerIndex = 0
				let selectedArr = data[e.target.value].split("/");
				this.dataList.stationId = selectedArr[0];
				this.dataList.machineId = selectedArr[1];
			}
			this.yj_searchForm(this.dataList);
		},
		TreeList(companyId, userId) {
			this.$http.getUserCompanyTreeList({
					companyId: companyId,
					userId: userId,
				})
				.then((res) => {
					if (res.errcode == 0) {
						this.treeData = this.tranListToTreeData(
							res.data.userCompanyTreeList
						);
						this.companyTree = this.treeData; //机构树
						// this.pickerIndex = 0;
						if (this.treeData[0].children == undefined) {
							console.log(11);
							this.treeName = this.treeData[0].fShortname;
							this.$refs.company._hide()
							// this.getExamine(this.treeData[0].fCompanyid);
							this.companyId = this.treeData[0].fCompanyid;
							this.treeName = this.treeData[0].fShortname;
							this.treeConfirm(this.treeData[0], 0);
						} else {
							console.log(22);
							var that = this
							uni.getStorage({
								key: 'userInfo',
								success: function(res) {
									that.userInfo = JSON.parse(res.data)
									// that.getExamine(that.userInfo.companyId);
									that.companyId = that.userInfo.companyId;
									that.treeName = that.userInfo.companyName;
								}
							})

						}
						this.dataList.fCompanyid = this.companyId
						this.yj_searchForm(this.dataList)

					} else {
						uni.showToast({
							title: "获取机构树失败",
							icon: 'none',
							duration: 2000
						});
						return false;
					}
				})

		},
		tranListToTreeData(data) {
			let result = [];
			if (!Array.isArray(data)) {
				return result;
			}
			// 清除已存在的children 重新赋值
			data.forEach((item) => {
				delete item.children;
			});

			let map = {};
			data.forEach((item) => {
				map[item.fCompanyid] = item;
			});

			data.forEach((item) => {
				let parent = map[item.fParentid];
				if (parent) {
					(parent.children || (parent.children = [])).push(item);
				} else {
					result.push(item);
				}
			});
			return result;
		},
		handFocus() {
			this.$refs.company._show();

		},
		// 查看
		yj_viewForm(e) {
			console.log("eeeee", e);
			uni.navigateTo({
				url: "/pages/apply/HntManage/inter/index?data=" + JSON.stringify(e.data) + "&companyId=" + this
					.companyId
			});
			return false;


		},
		submit() {
			var that = this
			this.Inv = 1
			// this.dataList.modelType = "1"
			this.yj_searchForm(this.dataList)

		},
		unsubmit() {
			var that = this
			this.Inv = 0
			// this.dataList.modelType = "0"
			this.yj_searchForm(this.dataList)

		},

		getLocalData() {
			var that = this
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					that.userInfo = JSON.parse(res.data);
					that.companyId = that.userInfo.companyId;
					that.TreeList(that.userInfo.companyId,
						that.userInfo.userId, );
				}
			})

		},
		callback() {},
		// 获取审核流程v 
		getExamine(data) {
			this.$http.getStationWarnByCompanyId({
					companyId: data
				})
				.then((res) => {
					if (res.errcode == 0) {
						console.log(12, res);
						this.AduitUser = res.data.yjQmpMsStationwarn;
					}
				})
				.catch((error) => {
					console.log(error);
				});
		},
		yj_searchForm1(num) {
			this.dataList.modelType = num
			this.yj_searchForm(this.dataList)
		},


		// 获取列表
		yj_searchForm(data) {
			var that = this;
			// let fAduitUser1 = [];
			// let fAduitUser2 = [];
			// let fAduitUser3 = [];
			// let fAduitUser4 = [];
			// console.log(12,this.AduitUser );
			// if (this.AduitUser != null) {
			// 	if(this.AduitUser.fAduitUser1!='----'){
			// 		fAduitUser1 = this.AduitUser.fAduitUser1.split("-");
			// 	}else {
			// 		fAduitUser1=[]
			// 	}
			// 	if(this.AduitUser.fAduitUser2!='----'){
			// 		fAduitUser2 = this.AduitUser.fAduitUser2.split("-");
			// 	}else {
			// 		fAduitUser2=[]
			// 	}
			// 	if(this.AduitUser.fAduitUser3!='----'){
			// 		fAduitUser3 = this.AduitUser.fAduitUser3.split("-");
			// 	}else {
			// 		fAduitUser3=[]
			// 	}
			// 	if(this.AduitUser.fAduitUser4!='----'){
			// 		fAduitUser4 = this.AduitUser.fAduitUser4.split("-");
			// 	}else {
			// 		fAduitUser4=[]
			// 	}

			// }

			// for (let index1 = 0; index1 < fAduitUser1.length; index1++) {
			// 	const element1 = fAduitUser1[index1];
			// 	if (this.userInfo.companyNature == element1) {
			// 		this.dataList.fCheckStatus = index1.toString();
			// 		break;
			// 	} else {
			// 		for (let index2 = 0; index2 < fAduitUser2.length; index2++) {
			// 			const element2 = fAduitUser2[index2];
			// 			if (this.userInfo.companyNature == element2) {
			// 				this.dataList.fCheckStatus = index2.toString();
			// 				break;
			// 			} else {
			// 				for (let index3 = 0; index3 < fAduitUser3.length; index3++) {
			// 					const element3 = fAduitUser3[index3];
			// 					if (this.userInfo.companyNature == element3) {
			// 						this.dataList.fCheckStatus = index3.toString();
			// 						break;
			// 					} else {
			// 						for (let index4 = 0; index4 < fAduitUser4.length; index4++) {
			// 							const element4 = fAduitUser4[index4];
			// 							if (this.userInfo.companyNature == element4) {
			// 								this.dataList.fCheckStatus = index4.toString();
			// 								break;
			// 							}
			// 						}
			// 					}
			// 				}
			// 			}
			// 		}
			// 	}
			// }



			this.$http.getClosedLists(data).then(res => {
				if (res.errcode == 0) {
					that.tableData = []
					that.tableData = res.data.pageInfo.list
					res.data.pageInfo.list.map((item, index) => {
						that.tableData[index]['level'] = item.F_WarningLevel == 0 ? '正常' : item
							.F_WarningLevel == 1 ? '一级预警' : item.F_WarningLevel == 2 ? '二级预警' : item
							.F_WarningLevel == 3 ? '三级预警' : item
							.F_WarningLevel == 4 ? '四级预警' : '五级预警'
					})
					console.log(that.tableData)
					// }, 1000)

				} else {
					console.log("接口报错")
				}
			}, (err) => {
				uni.showToast({
					title: "获取数据失败",
					icon: 'none',
					duration: 2000
				});
			})

		}

	}
}
